﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketEngine.Engine;
using PocketEngine.Font;
namespace PocketEngine.Gui
{
    public class pGuiManager
    {
        public static int MouseX, MouseY;
        public static bool[] MouseBut = new bool[5];
        public static bool[] KeyBut = new bool[255];
        public static pFont SystemFont = null;
        public static pWidget Root = null;
        public static void SetRoot(pWidget widget)
        {
            Root = widget;
        }
        public static void InitGui()
        {
            SystemFont = new pFont("FreeSans.ttf", 12);

        }
        public static void Update()
        {
            
            if (Root == null)
            {
                return;
            }

            MouseX = (int)pEngine.Input.GetMousePos().X;
            MouseY = (int)pEngine.Input.GetMousePos().Y;

            bool[] nm = new bool[4];
            bool[] nk = new bool[255];

            for (int i = 0; i < 4; i++)
            {
                nm[i] = pEngine.Input.GetMouseDown(i);
                if(nm[i]==true && MouseBut[i]==false)
                {
                    if(OverWidget!=null)
                    {
                        PressedWidget[i] = OverWidget;
                        OverWidget.OnMouseDown(i);

                        if(OverWidget!=ActiveWidget)
                        {
                            if(ActiveWidget!=null)
                            {
                                ActiveWidget.OnDeactivate();
                            }
                            OverWidget.OnActivate();
                        }
                    }
                }
                if(nm[i]==false && MouseBut[i]==true)
                {
                    if(PressedWidget[i]!=null)
                    {
                        PressedWidget[i].OnMouseUp(i);
                    }
                }
            }
            for (int i = 0; i < 255; i++)
            {
                nk[i] = pEngine.Input.GetKeyDown(i);
                if (nk[i] == true && KeyBut[i] == false)
                {
                    if (ActiveWidget!=null)
                    {
                        ActiveWidget.OnKeyDown(i);
                    }
                    if (OverWidget != null)
                    {
                        ActiveWidget.OnKeyDown(i);
                    }
                }
                if (KeyBut[i] == true && nk[i] == false)
                {
                    if (ActiveWidget != null)
                    {
                        ActiveWidget.OnKeyUp(i);

                    }
                    if (OverWidget != null)
                    {
                        OverWidget.OnKeyUp(i);
                    }
                }
            }

            KeyBut = nk;
            MouseBut = nm;

            ProcessWidget(Root);

            Root.UpdateWidget();
        }
        
     
        public static bool ProcessWidget(pWidget w)
        {
            foreach (var sw in w.Widgets) 
            {
                if (ProcessWidget(sw) == true)
                {
                    return true;
                }

            }

            if (w.InBounds(MouseX, MouseY))
            {
                if (OverWidget != null)
                {
                    if (OverWidget != w)
                    {
                        OverWidget.OnMouseLeave();
                    }
                }
                if (OverWidget != w)
                {
                    OverWidget = w;
                    OverWidget.OnMouseEnter();
                }
                return true;
            }
            return false;
        }
        
        public static pWidget ActiveWidget = null;
        public static pWidget[] PressedWidget = new pWidget[32];
        private static pWidget OverWidget = null;

        public static void DrawFast()
        {
            if (Root == null)
            {
                return;
            }
            Root.DrawWidget();
        }
    }
}
